﻿using System;
using UnityEditor;
using UnityEngine;

public class USPreferenceWindow : EditorWindow
{
    private static string captureFrameRatePref = "uSequencer-Prefs-CaptureFrameRate";
    private static string capturePathPref = "uSequencer-Prefs-CapturePath";
    private static int defaultCaptureFrameRate = 60;
    private static int defaultCaptureUpscaleMount = 1;
    private static float defaultEventEditorWidth = 100f;
    private static string defaultEventEditorWidthPref = "uSequencer-Prefs-DefaultEventEditorWidth";
    private static float defaultOSXZoomFactor = 0.001f;
    private static float defaultScrollFactor = 1f;
    private static float defaultWinZoomFactor = 0.01f;
    private static string scrollFactor = "uSequencer-Prefs-ScrollFactor-test";
    private static string upscaleAmountPref = "uSequencer-Prefs-UpscaleAmount";
    private static string zoomFactor = "uSequencer-Prefs-ZoomFactor-test";

    private static string GetDefaultCapturePath()
    {
        int startIndex = Application.dataPath.LastIndexOf("/");
        return (Application.dataPath.Remove(startIndex) + "/Output");
    }

    private void OnDestroy()
    {
        if (CapturePath.Length == 0)
        {
            Debug.LogError("Directory Path, specified in the uSequencer Preference Window is invalid, resetting to the default : " + GetDefaultCapturePath());
            CapturePath = GetDefaultCapturePath();
        }
    }

    private void OnGUI()
    {
        CapturePath = EditorGUILayout.TextField("Capture Path", CapturePath, new GUILayoutOption[0]);
        UpscaleAmount = EditorGUILayout.IntField("Upscale Amount", UpscaleAmount, new GUILayoutOption[0]);
        CaptureFrameRate = EditorGUILayout.IntField("Capture FrameRate", CaptureFrameRate, new GUILayoutOption[0]);
        DefaultEventEditorWidth = EditorGUILayout.FloatField("Default Event Editor Width", DefaultEventEditorWidth, new GUILayoutOption[0]);
        if (Application.platform == RuntimePlatform.OSXEditor)
        {
            ZoomFactor = EditorGUILayout.Slider("uSequencer Zoom Strength", ZoomFactor, 0f, 0.1f, new GUILayoutOption[0]);
        }
        else
        {
            ZoomFactor = EditorGUILayout.Slider("uSequencer Zoom Strength", ZoomFactor, 0f, 1f, new GUILayoutOption[0]);
        }
        ScrollFactor = EditorGUILayout.Slider("uSequencer Scroll Strength", ScrollFactor, 0f, 100f, new GUILayoutOption[0]);
    }

    [UnityEditor.MenuItem("Edit/uSequencer Preferences")]
    private static void OpenWindow()
    {
        EditorWindow.GetWindow(typeof(USPreferenceWindow));
    }

    public static int CaptureFrameRate
    {
        get
        {
            if (!EditorPrefs.HasKey(captureFrameRatePref))
            {
                EditorPrefs.SetInt(captureFrameRatePref, defaultCaptureFrameRate);
            }
            return EditorPrefs.GetInt(captureFrameRatePref);
        }
        private set
        {
            EditorPrefs.SetInt(captureFrameRatePref, value);
        }
    }

    public static string CapturePath
    {
        get
        {
            if (!EditorPrefs.HasKey(capturePathPref))
            {
                EditorPrefs.SetString(capturePathPref, GetDefaultCapturePath());
            }
            return EditorPrefs.GetString(capturePathPref);
        }
        private set
        {
            EditorPrefs.SetString(capturePathPref, value);
        }
    }

    public static float DefaultEventEditorWidth
    {
        get
        {
            if (!EditorPrefs.HasKey(defaultEventEditorWidthPref))
            {
                EditorPrefs.SetFloat(defaultEventEditorWidthPref, defaultEventEditorWidth);
            }
            return EditorPrefs.GetFloat(defaultEventEditorWidthPref);
        }
        private set
        {
            EditorPrefs.SetFloat(defaultEventEditorWidthPref, value);
        }
    }

    public static float ScrollFactor
    {
        get
        {
            if (!EditorPrefs.HasKey(scrollFactor))
            {
                EditorPrefs.SetFloat(scrollFactor, defaultScrollFactor);
            }
            return EditorPrefs.GetFloat(scrollFactor);
        }
        private set
        {
            EditorPrefs.SetFloat(scrollFactor, value);
        }
    }

    public static int UpscaleAmount
    {
        get
        {
            if (!EditorPrefs.HasKey(upscaleAmountPref))
            {
                EditorPrefs.SetInt(upscaleAmountPref, defaultCaptureUpscaleMount);
            }
            return EditorPrefs.GetInt(upscaleAmountPref);
        }
        private set
        {
            EditorPrefs.SetInt(upscaleAmountPref, value);
        }
    }

    public static float ZoomFactor
    {
        get
        {
            if (!EditorPrefs.HasKey(zoomFactor))
            {
                if (Application.platform == RuntimePlatform.OSXEditor)
                {
                    EditorPrefs.SetFloat(zoomFactor, defaultOSXZoomFactor);
                }
                else if (Application.platform == RuntimePlatform.WindowsEditor)
                {
                    EditorPrefs.SetFloat(zoomFactor, defaultWinZoomFactor);
                }
            }
            return EditorPrefs.GetFloat(zoomFactor);
        }
        private set
        {
            EditorPrefs.SetFloat(zoomFactor, value);
        }
    }
}

